#include <iostream>
#include <cstdio>
#include <string>
#include <unistd.h>
#include <pthread.h>

void showtid(pthread_t &tid)
{
    printf("tid: 0x%lx\n",tid);
}
std::string FormatId(const pthread_t &tid)
{
    char id[64];
    snprintf(id, sizeof(id), "0x%lx", tid);
    return id;
}
void *routine(void *args)
{
    std::string name = static_cast<const char*>(args);
    pthread_t tid = pthread_self();
    int cnt = 3;
    while(cnt)
    {
        std::cout << "我是一个新线程: my name: main thread "  << " 我的Id:  " << FormatId(tid) << std::endl;
        sleep(1);
        cnt--; 
    }
    return nullptr;
}

int main()
{
    pthread_t tid;//tid变量用于存储新创建进程的标识符
    int n = pthread_create(&tid, nullptr, routine, (void*)"thread-1");
    (void)n;
    showtid(tid);
    int cnt = 3;
    while(cnt)
    {
        std::cout << "我是main线程: my name: main thread " << " 我的Id:  " << FormatId(pthread_self()) << std::endl;
        sleep(1);
        cnt--; 
    }
    pthread_join(tid,nullptr);//等待进程结束
    return 0;
}




// int main()
// {
//     pthread_t tid;
//     pthread_create(&tid, nullptr, threadrun, (void*)"thread-1");
//     while(true)
//     {
//         std::cout << "我是主线程..." << ",pid: " << getpid() << std::endl;
//         sleep(1);
//     }
//     return 0;
// }

